Skip to main content

Banner

warning

This is the documentation for the Advanced API. Most developers should use our Simplified API (documented in Banner), which comes with several built-in features such as:

  • Automatic ad loading
  • Retries for failed requests
  • Simplified lifecycle management

Only use the Advanced API if you need fine-grained control over the ad loading and presentation lifecycle.

The Advanced API provides direct access to individual Banner instances, giving you complete control over when and how ads are loaded and displayed.

Create a Banner instance

val size = Banner.Size.Phone /* or Banner.Size.Tablet */
val banner = Banner.create(activity, "<placement-id>", size)

When creating a banner, in addition to your placementId, you have to provide:

  • size: Size of the banner. See Banner sizes for the available sizes.

Register ad callbacks

banner.listener = object : Banner.Listener {
override fun onPrebiddingFinished(result: PrebiddingResults) {
Log.d("Banner", "Banner client-side bidding finished!")
}

override fun onLoaded(loadResult: LoadResult) {
Log.d("Banner", "Banner loaded!")
}

override fun onFailedToLoad(loadError: LoadError, loadResult: LoadResult?) {
Log.d("Banner", "Banner failed to load. Reason: ${loadError.message}")
}

// Impression Callback
override fun onImpression(impressionData: ImpressionData) {
Log.d("Banner", "Banner impression, with revenue: ${impressionData.revenue}")
}
}

Load an ad

banner.load()

Load an ad (with custom properties)

banner.load(
buildCustomProperties {
addString("game_mode", "classic")
}
)

Displaying the banner

The most common practice is to add the banner view to the view hierarchy either:

  • After creating the Banner instance.
  • After the onLoaded(loadResult) listener callback is called.
val bannerContainer = findViewById<ViewGroup>(R.id.bannerContainer)
val bannerView = banner.view

// Optionally, set the ad space where the banner will be shown
banner.setAdSpace("banner-ad-space")

bannerContainer.addView(bannerView)
ConstantSize (WxH)Description
Phone320x50Size for banners used in phone devices
Tablet728x90Size for banners used in tablet devices
MREC300x250Size for IAB Medium Rectangle banners

Size is in DP for Android.

After being displayed for some time, our banner fires a load() call automatically to refresh its contents. You can configure this time through our Admin tool for each one of your banners.

Our banner has a built-in auto retry for failed load attempts. This means that when a banner fails to load, it will retry again until it loads successfully. Time between each retry attempt will increase using an exponential backoff. You should not add any retry logic on your end, as it may interfere with our retry behaviour.

Complete example

BannerSample.kt
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.etermax.xmediator.core.api.Banner
import com.etermax.xmediator.core.api.entities.ImpressionData
import com.etermax.xmediator.core.api.entities.LoadError
import com.etermax.xmediator.core.api.entities.LoadResult
import com.example.yourapp.R

class BannerSample : AppCompatActivity() {
private var banner: Banner? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_banner)

findViewById<Button>(R.id.button_load).setOnClickListener {
Log.d("BannerSample", "Load button clicked")

// Dispose previous banner instance
banner?.destroy()
findViewById<ViewGroup>(R.id.banner_container).removeAllViews()

// Create a banner instance
banner = Banner.create(
activity = this,
placementId = "<placement-id>",
size = Banner.Size.Phone,
)

// Add banner to view hierarchy
findViewById<ViewGroup>(R.id.banner_container).addView(banner?.view)

// Setup callbacks
banner?.listener = object : Banner.Listener {
override fun onLoaded(loadResult: LoadResult) {
Log.d("BannerSample", "Loaded!")
}

override fun onFailedToLoad(loadError: LoadError, loadResult: LoadResult?) {
Log.d("BannerSample", "Failed to load. Reason: ${loadError.message}")
}

override fun onImpression(impressionData: ImpressionData) {
Log.d("BannerSample", "Impression with revenue: ${impressionData.revenue}")
}
}

// Request an ad
banner?.load()
}
}
}